import { defineConfig, loadEnv, ConfigEnv } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { resolve } from 'path';
import AutoImport from 'unplugin-auto-import/vite';
// import Components from 'unplugin-vue-components/vite'
// https://vitejs.dev/config/

export default defineConfig((mode: ConfigEnv) => {
	const env = loadEnv(mode.mode, process.cwd());
	return {
		build: {
			target: 'es6',
		},
		resolve: {
			alias: [
				{
					find: '@',
					replacement: resolve(__dirname, 'src'),
				},
			],
		},
		server: {
			port: env.VITE_PORT as unknown as number,
			// 选项写法
			proxy: {
				'/api': {
					target: env.VITE_API_URL,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, ''),
				},
			},
		},
		plugins: [
			uni(),
			vueJsx(),
			AutoImport({
				// 使用
				imports: ['vue'],
				dts: 'src/auto-import.d.ts',
				// 如有用到eslint记得加上写段，没有用到可以忽略
				eslintrc: {
					enabled: true,
				},
			}),
		],
	};
});
